var tradeGoodsInfo = null;

function getTypeInfo() {
  var api = "view/api/get_type_api.php";
  comAjax(api, function (result) {
    var data = JSON.parse(result).data;
    var html = '<select name="type" class="form-control manager-add-status-selector">';
    for (var index = 0; index < data.length; index++) {
      html = html + ' <option value="' + data[index].type + '">' + data[index].type_name + '</option>';
    }
    html = html + '  </select>';
    $(".customer-type-selector").html(html);
  });
}


function updateData() {
  if (!validate("goods-info-edit-form")) {
    return false;
  }
  var form = $("#goods-info-edit-form").serialize();
  console.log(form);
  comAjax("view/api/update-goods-info-api.php", function (result) {
    var data = result.data;
    if (!data.result) {
      layer.msg('失败!', {icon: 5});
    }
    else {
      $(".bs-trade-edit-modal-lg").modal("hide");
      if (!isEmpty(tradeGoodsInfo)) {
        refreshGoodsInfoList();
      }
    }
  }, form);
}

function registData() {
  if (!validate("goods-info-regist-form")) {
    return false;
  }
  var form = $("#goods-info-regist-form").serialize();
  // console.log(form);
  comAjax("view/api/add-goods-info-api.php", function (result) {
    var data = result.data;
    if (!data.result) {
      layer.msg('失败!', {icon: 5});
    }
    else {
      $(".bs-trade-add-modal-lg").modal("hide");
      if (!isEmpty(tradeGoodsInfo)) {
        refreshGoodsInfoList();
      }
    }
  }, form);
}

function actions(element) {
  if ($(element).hasClass("customer-delete")) {
    layer.confirm('确定删除？', {
      btn: ['确定','取消']
    }, function(){
      var uuid = $(element).attr("data-uuid");
      comAjax("view/api/delete_goods.php", function (result) {
        refreshGoodsInfoList();
      }, "uuid=" + uuid);
    }, function(){
//
    });
  }
  else if ($(element).hasClass("customer-edit")) {
    var data = $(element).data();
    // console.log(data);
    $("#edit-uuid").val(data.uuid);
    $("#edit_name").val(data.name);
    $("#edit_qr_code").val(data.qrCode);
    $("#edit_arrived_time").val(data.arrivedTime);
    $("#edit_purchase_time").val(data.purchaseTime);
    $("#edit_produce_time").val(data.produceTime);
    $("#edit_expire_time").val(data.expireTime);
    $("#edit_count").val(data.count);
    $("#edit_comment").val(data.comment);

  }
}

function refreshGoodsInfoList() {
  if (isEmpty(tradeGoodsInfo)) {
    initTable();
    $("#tradeGoodsInfo_length").addClass("col-md-6");
    $("#tradeGoodsInfo_paginate").addClass("col-md-6");
  }
  else {
    tradeGoodsInfo.ajax.reload();
  }
  initDatePicker();
}
